;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname pickgame-client) (read-case-sensitive #f) (teachpacks ((lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#f constructor mixed-fraction #f #f none #f ((lib "universe.ss" "teachpack" "2htdp")))))
;; CLIENT IMPLEMENTATION OF THE PICKGAME

;; A game is:
(define-struct game (boxes players mycolor))
;; boxes is a list of symbols representing who owns the given boxes
;; players is a list of symbols representing the players
;; mycolor is my color

;; IMPORTANT ETC
(define BASE-SCENE (empty-scene 500 50))
(define Y-CENTER 25)
(define BASE-GAME (make-game empty empty 'black))
;;;; DRAWING FUNCTIONS

;; draw-game : game -> scene
;; draws a scene from the given game.
;; (the current state of the game)
(define (draw-game w)
  (draw-boxes (game-boxes w) 0 BASE-SCENE))


;; draw-boxes : [listof box] scene -> scene
;; draws the list of boxes onto the scene.
(define (draw-boxes lob n sc)
  (cond [(empty? lob) sc]
        [(symbol=? 'unowned (first lob))
         (draw-boxes (rest lob) (add1 n) sc)]
        [else  (place-image (rectangle 50 50 'solid (first lob))
                            (+ 25 (* 50 n)) 
                            Y-CENTER 
                            (draw-boxes (rest lob) (add1 n) sc))]))


;; handle-new-msg : game sexp -> game
;; handles the receipt of a new sexp from the server.
(define (handle-new-msg game sexp)
  (cond [(and (symbol? (first sexp)) (symbol=? 'message (first sexp)))
         game]
        [(symbol? (first sexp)) (make-game empty empty (first sexp))]
        [else (make-game (first sexp) (second sexp) (game-mycolor game))]))
(check-expect (handle-new-msg (make-game 1 2 3) (list 'message)) (make-game 1 2 3))
(check-expect (handle-new-msg (make-game 1 2 3) (list 'anything?)) (make-game empty empty 'anything?))



;; handle-mouse-click : game number number mouse-event -> package
(define (handle-mouse-click w x y evt)
  (if (not (symbol=? evt 'button-down))
      w
      (make-package (make-game (game-boxes w)
                                (game-players w)
                                (game-mycolor w))
                    (list (get-box-number x)))))
(check-expect (handle-mouse-click (make-game 1 2 3) empty empty 'not-working)
              (make-game 1 2 3))
(check-expect (handle-mouse-click (make-game 1 2 3) 150 0 'button-down)
              (make-package (make-game 1 2 3) (list 3)))


;; get-box-number : number -> number
;; determines which box the user clicked on.
;; based on the x-position of the mouse click
(define (get-box-number n)
  (ceiling (/ n 50)))
(check-expect (get-box-number 50) 1)
(check-expect (get-box-number 200) 4)



#;
(big-bang BASE-GAME
          (register "127.0.0.1")
          (on-receive handle-new-msg)
          (on-draw draw-game)
          (on-mouse handle-mouse-click))